<?php
	$amazon_zone="com";
	$amazon_api="AKIAINCVELGD65NBPHOA"; //api key
	$amazon_apix="IeuD0udg7CebzlEcqQhY2MXmahCA1L3xdeozyVmb"; //Secret key
	$offerlink="";
	$amazon_acc="brothercs6000i0b1-20";
	$amazon_asin="B0018TBECU";
	$posts="0";
	//$link_cloak=true;
	$wp_themes =="best";
	require_once "amz_functions.php";
	require_once "test_admin.php";
	require_once "simplehtmldom/simple_html_dom.php";
	
	
/*function amazon_get_url($url) {
	
	if( strtolower(ini_get('allow_url_fopen')) == "on" ) {
		$data = file_get_contents($url);
	} else {
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
		curl_setopt($ch, CURLOPT_URL, $url);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}
}
function amazon_request($str) {	
	global $amazon_zone, $amazon_api, $amazon_apix;
	
	$rest_param = 'Service=AWSECommerceService&AWSAccessKeyId='.$amazon_api.'&'.$str.
			'&Version=2009-01-06&Timestamp='.gmdate("Y-m-d\TH:i:s\Z");
	$rest_param = str_replace(",","%2C",$rest_param);
	$rest_param = str_replace(":","%3A",$rest_param);
	
	$rest_param_arr = explode('&', $rest_param);
	sort($rest_param_arr);
	$rest_param = implode('&', $rest_param_arr);
	
	$rest_prepend = "GET\necs.amazonaws.".$amazon_zone."\n/onca/xml\n".$rest_param;
	$signature = base64_encode(hash_hmac("sha256", $rest_prepend, $amazon_apix, True));
	$signature = urlencode($signature);

	return "http://ecs.amazonaws.".$amazon_zone."/onca/xml?".$rest_param."&Signature=".$signature;
}
*/

$request_dirty='ItemId='.$amazon_asin.'&MerchantId=Featured&Operation=ItemLookup&ResponseGroup=Medium%2COfferFull&Sort=titlerank';
$request = amazon_request($request_dirty);
$response = amazon_get_url($request);
$parsed_xml = simplexml_load_string($response);
	
	
//echo "<pre>$parsed_xml</pre>";
//compare('B004ZEWOKG','');



	$itemObj = array();
	$count=0;
	foreach( $parsed_xml->Items->Item as $item) {
		$itemObj[$count] = $item;
		//print_r($itemObj);
		$count++;
	}

print_r($parsed_xml->Error->Message);





//########################################edit by p'ping########################################################
function amazon_request_review($asin) {	
	global $amazon_zone, $amazon_api, $amazon_apix,$amazon_acc;
	
	
	$rest_prepend = "GET\necs.amazonaws.".$amazon_zone."\n/onca/xml\n".$rest_param;
	$signature = base64_encode(hash_hmac("sha256", $rest_prepend, $amazon_apix, True));
	$signature = urlencode($signature);

	
	return "http://www.amazon.".$amazon_zone."/reviews/iframe?akid=".$amazon_api."&alinkCode=xm2&summary=0&truncate=9216&v=5&asin=".$asin."&atag=".$amazon_acc."&exp=".gmdate("Y-m-d\TH:i:s\Z")."&sig=".$signature;
}
//########################################edit by p'ping########################################################






$buyUrl = 'http://www.amazon.com/gp/product/'.$amazon_asin.'/?tag='.$amazon_acc;


?>
<?php

function amazon_the_title() {
	echo get_amazon_the_title();
}
function get_amazon_the_title() {
	global $itemObj, $posts;
	$item = $itemObj[$posts];
	$title = my_encode($item->ItemAttributes->Title);
	$title = substr($title, 0, 72);
	return $title;	
}
function amazon_the_permalink() {
	global $itemObj, $posts, $home_url;
	$item = $itemObj[$posts];
	echo $home_url.'/detail/'.amazon_get_permalink($item->ASIN, $item->ItemAttributes->Title);
}

?>

<?php
		$title = $item->ItemAttributes->Title;
		/*$title = str_replace(","," - ", $title);
		$title = str_replace("/"," ", $title);
		$title = str_replace("  "," ", $title);
		$title = str_replace("  "," ", $title);
		$title = str_replace("  "," ", $title);*/
				
		if( isset($item->EditorialReviews) ){
			$review = $item->EditorialReviews->EditorialReview->Content;
		} else {
			$review = $item->Description;
		}
		$aws_review = $item->CustomerReviews->Review;
		$aws_feature = $item->ItemAttributes->Feature;

		//if( $link_cloak ) $buyUrl = $home_url.'/more.php?asin='.$item->ASIN;
		if( $link_cloak ) $buyUrl = $home_url.'/more-'.$item->ASIN.'.html';
		else $buyUrl = 'http://www.amazon.'.$amazon_zone.'/gp/product/'.$item->ASIN.'/?tag='.$amazon_acc;
						
		#Offer link
		if( $link_cloak ) $offerlink = $home_url.'/offer.php?asin='.$item->ASIN;
		//if( $link_cloak ) $offerlink = $home_url.'/offer-'.$item->ASIN.'.html';
		else $offerlink = 'http://www.amazon.'.$amazon_zone.'/gp/offer-listing/'.$item->ASIN.'/?tag='.$amazon_acc;
			
		#AddCart Link
		//if( $link_cloak ) $addcart = $home_url.'/buy.php?asin='.$item->ASIN;
		if( $link_cloak ) $addcart = $home_url.'/buy-'.$item->ASIN.'.html';
		else $addcart = 'http://www.amazon.com/gp/aws/cart/add.html?AssociateTag='.$amazon_acc.'&ASIN.1='.$item->ASIN.'&Quantity.1=1';
		
		//#############################edit by bird#########################################
		/*if ($xConfig["link"]=="direct")
			$detailDirect = $addcart;
		else if ($xConfig["link"]=="detail")
			$detailDirect = $buyUrl;*/
		//#############################edit by bird#########################################
					
		#Show Manufacturer
		$manufacturer = $item->ItemAttributes->Manufacturer;
		$totalreviews=$item->CustomerReviews->TotalReviews;
		$linkreviews=$item->ItemLinks->ItemLink[5]->URL;

		#Review Link
		if( $link_cloak ) $linkreviews = $home_url.'/reviews-'.$item->ASIN.'.html';
		else $linkreviews = 'http://www.amazon.'.$amazon_zone.'/review/product/'.$item->ASIN.'?tag='.$amazon_acc;
		
		#Check Product Customer Review
		if(!empty($item->CustomerReviews))	{
			$cus="&nbsp;<a class='textunderline' href='$linkreviews' target='_blank' rel='nofollow'>"."($totalreviews customers reviews)"."</a>";
		}
		else	{
			$cus="&nbsp;<a class='textunderline' href='$linkreviews' target='_blank' rel='nofollow'>"."Be the first "."</a>";
		}
		$totalreviews = $cus;
			
		#Available and ship	
		$Offer_IsEligibleForSuperSaverShipping = $item->OfferListing->AvailabilityAttributes->IsEligibleForSuperSaverShipping; 
		if($Offer_IsEligibleForSuperSaverShipping=="1"){
			$Offer_Is=" <font color='green'>Is Eligable for Super Save Shipping<br/></font>";
		} else {
			$Offer_Is=" <font><br/>Get best price order now </font>";
		} 		

		#See all customer review	
		$allreviews=$item->CustomerReviews->TotalReviews;
		if(!empty($item->CustomerReviews))	{
				$seeall="<a class='textunderline' href='$linkreviews' target='_blank' rel='nofollow'>"."See all $allreviews reviews"." &gt;&gt;</a>";
		}
		$seeallreviews = $seeall;
			
		#Average Product Customer Review
		$averagerating=$item->CustomerReviews->AverageRating;
		

		#Feature
		$fea=array($item->ItemAttributes->Feature[0],
			   $item->ItemAttributes->Feature[1],
			   $item->ItemAttributes->Feature[2],
			   $item->ItemAttributes->Feature[3],
			   $item->ItemAttributes->Feature[4]);
		function fea($f)
		{
			if(!empty($f))
			{$tu="<li>"."$f"."</li>";}
			return $tu;	
		}		   
		$feature="<ul>".fea($fea[0]).fea($fea[1]).fea($fea[2]).fea($fea[3]).fea($fea[4])."</ul>";
		
		#More Image
		$imgset=array(//$item->ImageSets->ImageSet[0]->LargeImage->URL,
                          $item->ImageSets->ImageSet[1]->LargeImage->URL,
                          $item->ImageSets->ImageSet[2]->LargeImage->URL,
                           $item->ImageSets->ImageSet[3]->LargeImage->URL,
                            $item->ImageSets->ImageSet[4]->LargeImage->URL,
                            $item->ImageSets->ImageSet[5]->LargeImage->URL);
		function imgset($im)
       {
          if(!empty($im))
         {$ti="<a target ='_blank' href='$im' rel='nofollow' class=\"highslide\" onclick=\"return hs.expand(this)\"><img src='"."$im"."' width='100'  style='margin:2px;' /></a> ";}
              return $ti;	
          }	
		 $moreimage="<br/>".imgset($imgset[0]).imgset($imgset[1]).imgset($imgset[2]).imgset($imgset[3]).imgset($imgset[4])."";
	
		
		#
		$totalnew=$item->OfferSummary->TotalNew;
		$availability= avai($totalnew,$offerlink);

		$img140 = item_get_image140($item);

		$img = item_get_image($item);
		//$img = item_get_thumb($item);

	
?>


			<div class="product" >
				<div class="img-box"> 
					<div class="smallImage">
                    <img  title="<?php echo clean($title); ?>" src="<?php echo $img->URL; ?>" alt="<?php echo clean($title); ?>" class="alignleft" />
                    
						<a href="<?php echo $img->URL; ?>" target='_blank' rel='nofollow' class="highslide" onclick="return hs.expand(this)">
<?
			#check Image
			if ($img!=null) {
?>
		<img  title="<?php echo clean($title); ?>" src="<?php echo $img->URL; ?>" alt="<?php echo clean($title); ?>" width="<?php echo $img->Width; ?>" height="<?php echo $img->Height; ?>" class="alignleft" />
<?php
			} else {
			
			}							
?>
						</a>						
					</div>
				</div>
			
			</div>		
			<div class="description">
				<div class="best_item_title">
					<h2>
						<!--<a href="<?php echo $buyUrl ?>" target='_blank'><?php echo $title; ?></a>-->
						<span onclick="javascript:location='<?php echo $buyUrl ?>'"><?php echo $title; ?></span>
					</h2>
				</div>

				
				<div class="amazonblurb">
                <?php 
				//echo $review;
				//echo $test = truncateStr($review,350);?>

                
                <br /><br class="clear"/>
				<div id="manufacturer"><b>Product By</b> <?php echo $manufacturer; ?></div>
						<span>Buy <?php echo $title; ?>  Ship with Fast and Save Shipping. Order Now Before Price Up.</span>					
				</div>
	
				<br/>
				<div class="pricemenu">
				
				
				<?php 
				$price = item_list_price($item);
				$lowprice = item_low_price($item); 
				?>
                
                
               		 <?php if($price != $lowprice){?>
					<b class="textprice">List Price:</b><span class="listprice"> <?php echo $price; ?> </span><br />
                    <?php }?>
					<b class="textprice">Sale Price:</b><span class="lowprice"> <?php echo $lowprice; ?> </span><br />
                    
                     <?php 
					 
						 $the_saveprice= $item->Offers->Offer->OfferListing->AmountSaved->FormattedPrice;
						$the_savepercent= $item->Offers->Offer->OfferListing->PercentageSaved;
						
						if($the_saveprice)
						{
							echo '<b class="textprice">You Save:</b><span class="saveprice">';
							echo $the_saveprice.'&nbsp;(&nbsp;';
							echo $the_savepercent;
							echo '%&nbsp;)</span><br />';
						}
					 $the_price = substr($price, 1);
					 $the_lowprice = substr($lowprice, 1);
					
					  $the_price=str_replace(',','',$the_price);
					  $the_lowprice=str_replace(',','',$the_lowprice);
					
					//echo intval($the_price);
					
					//echo $the_price = number_format($the_price,2,'.',',');
					//echo $the_lowprice = number_format($the_lowprice,2,'.','');
					
					//$the_price = number_format($the_price,2,'.','');
					//$the_price = number_format($the_price,2,'.','');
					
					if(is_numeric($the_lowprice) && is_numeric($the_price && $the_lowprice != $the_price )){
					$the_saveprice = $the_price-$the_lowprice;
					$the_savepercent = ($the_saveprice/$the_price)*100;
					
					
					echo '<b class="textprice">You Save:</b><span class="saveprice">$';
					//echo round($the_saveprice ,2);
					//echo $the_saveprice;
					
					echo number_format($the_saveprice, 2, '.', ',');
					
					echo '&nbsp;(&nbsp;';
					echo round($the_savepercent);
					echo '%&nbsp;)</span><br />';
					
					
					};
					
					
					?>
					<span class="freeship"> <?php echo $Offer_Is; ?> </span>
				</div>
				<br />
		
				<div class="topaverage_bestlist">
					<b>Average User Rating:</b><br />	
								<?php echo compare_rating2($item->ASIN,$offerlink) ;?>
					
				</div>
			
				<br />
				
				<span class="amazon-link2">
					<span onclick="javascript:location='<?php echo $addcart; ?>'" class="accesory_similar">
						<img src="<?php echo $home_url; ?>/images/buttons/buy-amazon.gif" />
					</span>
				</span>
				
			</div>
							
			<br />
			<div id="tabs" style="float:left;">
			<div id="getprod_tabs">
				<div id="getprod_wrap">
				<!-- tabs -->
				<ul class="getprod">
					<li class="getprod first"><a href="#compare" class="back">Compare Prices</a></li>
					<li class="getprod"><a href="#description" class="back">Product Details</a></li>
					<li class="getprod"><a href="#gallery" class="back">Feature</a></li>
					<li class="getprod"><a href="#review" class="back">User Reviews</a></li>
					<li class="getprod"><a href="#keyword_search_engine" class="back">Keyword</a></li>

				</ul>
				</div>
			</div>
				

			
			<div id="compare" class="tab_content">
			<br />
				<div class="item_custom_top">Where to Buy</div>
				<div class="item_custom_middle">
				<?php //echo $stories ;?>         
				<?php echo compare($item->ASIN,$offerlink) ;?>
				</div>
				<div class="item_custom_bottom"><?php echo strip_tags($availability); ?></div>
			</div>
			
			<div id="description" class="tab_content">
				<div class="headtextbg alignleft">
				<b>Manufacturer Description:</b><br /><br />
					<?php echo $review; ?>..<span onclick="javascript:location='<?php echo $buyUrl; ?>'" class="accesory_similar">Read more</span><br /> <br class="clear"/>
				</div>
				

						
			</div>
			<div id="gallery" class="tab_content">
				<div class="headtextbg alignleft">
				<b>Product Feature:</b><br />
					<?php echo $feature; ?>
				</div>
				<br />
				<div class="aligncenter">
				<?php echo $moreimage;?>
				</div>
			</div>
			
			<div id="review" class="tab_content">
					<div class="headtextbg alignleft">
					<script language="javascript">
						$(document).ready(function()
						{
							$("div#review a").attr('rel', 'nofollow');
							//$("div#review a").before("<span>" + this.innerHTML + "</span>");
							//$("div#review a").append('<span>'+$(this).html()+'</p>');
						});
					</script>
<?php
	$url_review = amazon_request_review($item->ASIN);
	$html = file_get_html($url_review);
	foreach($html->find('table.crIFrameReviewList') as $e)
	{
		//echo $e->innertext;
		echo strip_tags ( $e->innertext, "<div><span><b><img><br><nobr>" );
	}


?>
			
				<div class="seeallreviews"><?php echo $seeallreviews; ?></div><br />
				</div>
			</div>
			
			
			
			<?php display_accesory($item)?>
			
			<?php display_similar($item)?>

		</div>
			<?php display_tags($item);?>
				
		
            
